package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.common.exception.LeaseException;
import com.atguigu.lease.common.result.ResultCodeEnum;
import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.model.enums.ReleaseStatus;
import com.atguigu.lease.web.admin.mapper.*;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentDetailVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentQueryVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentSubmitVo;
import com.atguigu.lease.web.admin.vo.fee.FeeValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【apartment_info(公寓信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class ApartmentInfoServiceImpl extends ServiceImpl<ApartmentInfoMapper, ApartmentInfo>
        implements ApartmentInfoService {

    @Autowired
    private GraphInfoService graphInfoService;
    @Autowired
    private ApartmentFacilityService apartmentFacilityService;
    @Autowired
    private ApartmentLabelService apartmentLabelService;
    @Autowired
    private ApartmentFeeValueService apartmentFeeValueService;
    @Autowired
    private ApartmentInfoMapper apartmentInfoMapper;
    @Autowired
    private GraphInfoMapper graphInfoMapper;
    @Autowired
    private LabelInfoMapper labelInfoMapper;
    @Autowired FacilityInfoMapper facilityInfoMapper;
    @Autowired
    private FeeValueMapper feeValueMapper;
    @Autowired
    private ApartmentLabelMapper apartmentLabelMapper;
    @Autowired
    private ApartmentFacilityMapper apartmentFacilityMapper;
    @Autowired
    private RoomInfoMapper roomInfoMapper;

    // 保存或者更新公寓信息
    @Override
    public void saveOrUpdateApartment(ApartmentSubmitVo apartmentSubmitVo) {
        boolean isUpdate = apartmentSubmitVo.getId() != null;
        super.saveOrUpdate(apartmentSubmitVo);
        if (!isUpdate) {
            // 删除图片列表 根据图片所属类型和所属对象ID来删除
            LambdaQueryWrapper<GraphInfo> graphInfoQueryWrapper = new LambdaQueryWrapper<>();
            graphInfoQueryWrapper.eq(GraphInfo::getItemType, ItemType.APARTMENT);
            graphInfoQueryWrapper.eq(GraphInfo::getItemId, apartmentSubmitVo.getId());
            graphInfoService.remove(graphInfoQueryWrapper);

            // 删除配套列表 根据配套所属对象ID来删除
            LambdaQueryWrapper<ApartmentFacility> apartmentFacilityQueryWrapper = new LambdaQueryWrapper<>();
            apartmentFacilityQueryWrapper.eq(ApartmentFacility::getApartmentId, apartmentSubmitVo.getId());
            apartmentFacilityService.remove(apartmentFacilityQueryWrapper);

            // 删除标签列表 根据标签所属对象ID来删除
            LambdaQueryWrapper<ApartmentLabel> apartmentLabelQueryWrapper = new LambdaQueryWrapper<>();
            apartmentLabelQueryWrapper.eq(ApartmentLabel::getApartmentId, apartmentSubmitVo.getId());
            apartmentLabelService.remove(apartmentLabelQueryWrapper);

            // 删除杂费列表 根据杂费所属对象ID来删除
            LambdaQueryWrapper<ApartmentFeeValue> feeValueQueryWrapper = new LambdaQueryWrapper<>();
            feeValueQueryWrapper.eq(ApartmentFeeValue::getApartmentId, apartmentSubmitVo.getId());
            apartmentFeeValueService.remove(feeValueQueryWrapper);
        }

        // 插入图片列表
        List<GraphInfo> graphInfoList = new ArrayList<>();
        apartmentSubmitVo.getGraphVoList().forEach(graphVo -> {
            GraphInfo graphInfo = new GraphInfo();
            BeanUtils.copyProperties(graphVo, graphInfo);
            graphInfo.setItemType(ItemType.APARTMENT);
            graphInfo.setItemId(apartmentSubmitVo.getId());
            graphInfoList.add(graphInfo);
        });
        graphInfoService.saveBatch(graphInfoList);


        // 插入配套列表
        List<ApartmentFacility> apartmentFacilities = new ArrayList<>();
        apartmentSubmitVo.getFacilityInfoIds().forEach(facilityid -> {
            ApartmentFacility apartmentFacility = ApartmentFacility.builder()
                    .facilityId(facilityid)
                    .apartmentId(apartmentSubmitVo.getId())
                    .build();
            apartmentFacilities.add(apartmentFacility);
        });
        apartmentFacilityService.saveBatch(apartmentFacilities);

        // 插入标签列表
        List<ApartmentLabel> apartmentLabelList = new ArrayList<>();
        apartmentSubmitVo.getLabelIds().forEach(labelid -> {
            ApartmentLabel apartmentLabel = ApartmentLabel.builder()
                    .labelId(labelid)
                    .apartmentId(apartmentSubmitVo.getId())
                    .build();
            apartmentLabelList.add(apartmentLabel);
        });
        apartmentLabelService.saveBatch(apartmentLabelList);

        // 插入杂费列表
        List<ApartmentFeeValue> apartmentFeeValueList = new ArrayList<>();
        apartmentSubmitVo.getFeeValueIds().forEach(feeValueId -> {
            ApartmentFeeValue apartmentFeeValue = ApartmentFeeValue.builder()
                    .feeValueId(feeValueId)
                    .apartmentId(apartmentSubmitVo.getId())
                    .build();
            apartmentFeeValueList.add(apartmentFeeValue);
        });
        apartmentFeeValueService.saveBatch(apartmentFeeValueList);

    }

    @Override
    public IPage<ApartmentItemVo> pageApartmentItemByQuery(IPage<ApartmentItemVo> apartmentItemVoPage, ApartmentQueryVo queryVo) {
        return apartmentInfoMapper.pageApartmentItemByQuery(apartmentItemVoPage,queryVo);
    }

    @Override
    public ApartmentDetailVo getApartmentDetailVo(Long id) {
        // 根据公寓ID查询公寓信息
        ApartmentInfo apartmentInfo = apartmentInfoMapper.selectById(id);
        if (apartmentInfo == null) {
            return null;
        }
        // 查询图片 GraphVo
        List<GraphVo> graphVoList = graphInfoMapper.selectGraphVosByItemAndId(ItemType.APARTMENT ,id);

        // 查询标签  LabelInfo
        List<LabelInfo> labelInfos = labelInfoMapper.selectLabelInfosByApartmentId(id);

        // 查询配套  FacilityInfo
        List<FacilityInfo> facilityInfoList = facilityInfoMapper.selectFacilityInfosByApartmentId(id);

        // 查询杂费   FeeValueVo
        List<FeeValueVo> feeValueVoList = feeValueMapper.selectfeeValueVoListByApartmentId(id);

        ApartmentDetailVo apartmentDetailVo = new ApartmentDetailVo();
        BeanUtils.copyProperties(apartmentInfo,apartmentDetailVo);
        apartmentDetailVo.setGraphVoList(graphVoList);
        apartmentDetailVo.setLabelInfoList(labelInfos);
        apartmentDetailVo.setFacilityInfoList(facilityInfoList);
        apartmentDetailVo.setFeeValueVoList(feeValueVoList);

        return apartmentDetailVo;
    }

    // 根据id删除公寓信息
    @Override
    public void removeApartmentById(Long id) {
        LambdaQueryWrapper<RoomInfo> roomInfoWrapper = new LambdaQueryWrapper<>();
        roomInfoWrapper.eq(RoomInfo::getApartmentId,id);
        Long count = roomInfoMapper.selectCount(roomInfoWrapper);
        if (count>0){
            throw new LeaseException(ResultCodeEnum.DELETE_ERROR);
        }

        apartmentInfoMapper.deleteById(id);
        //1.删除GraphInfo
        LambdaQueryWrapper<GraphInfo> graphInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemType,ItemType.APARTMENT);
        graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemId,id);
        graphInfoMapper.delete(graphInfoLambdaQueryWrapper);
        //2.删除ApartmentLabel
        LambdaQueryWrapper<ApartmentLabel> apartmentLabelLambdaQueryWrapper = new LambdaQueryWrapper<>();
        apartmentLabelLambdaQueryWrapper.eq(ApartmentLabel::getApartmentId,id);
        apartmentLabelMapper.delete(apartmentLabelLambdaQueryWrapper);
        //3.删除ApartmentFacility
        LambdaQueryWrapper<ApartmentFacility> apartmentFacilityLambdaQueryWrapper = new LambdaQueryWrapper<>();
        apartmentFacilityLambdaQueryWrapper.eq(ApartmentFacility::getApartmentId,id);
        apartmentFacilityMapper.delete(apartmentFacilityLambdaQueryWrapper);
        //4.删除ApartmentFeeValue
        LambdaQueryWrapper<ApartmentFeeValue> apartmentFeeValueLambdaQueryWrapper = new LambdaQueryWrapper<>();
        apartmentFeeValueLambdaQueryWrapper.eq(ApartmentFeeValue::getApartmentId,id);
        apartmentFeeValueService.remove(apartmentFeeValueLambdaQueryWrapper);
    }

    // 根据id修改公寓发布状态
    @Override
    public void updateReleaseStatusById(Long id, ReleaseStatus status) {
        LambdaUpdateWrapper<ApartmentInfo> apartmentInfoLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        apartmentInfoLambdaUpdateWrapper.eq(ApartmentInfo::getId,id);
        apartmentInfoLambdaUpdateWrapper.set(ApartmentInfo::getIsRelease,status);
        super.update(apartmentInfoLambdaUpdateWrapper);
    }

    // 根据区县id查询公寓信息列表
    @Override
    public List<ApartmentInfo> listInfoByDistrictId(Long id) {
        LambdaQueryWrapper<ApartmentInfo> apartmentQueryWrapper = new LambdaQueryWrapper<>();
        apartmentQueryWrapper.eq(ApartmentInfo::getDistrictId,id);
        List<ApartmentInfo> apartmentInfoList = apartmentInfoMapper.selectList(apartmentQueryWrapper);

        return apartmentInfoList;
    }


}




